package cn.csq.linked.ques;

/**
 * @author <a href="mailto:csq02198031@alibaba-inc.com">chengshanqing</a>
 * @date 2025/8/22 14:54
 */
public class Test21 {

    public ListNode21 mergeTwoLists(ListNode21 l1, ListNode21 l2) {
        ListNode21 dummy = new ListNode21(), p = dummy;
        ListNode21 p1 = l1;
        ListNode21 p2 = l2;

        while (p1 != null && p2 != null) {
            // 比较p1 p2两个指针
            // 将值较小的节点接到p指针
            if (p1.val > p2.val) {
                p.next = p2;
                p2 = p2.next;
            } else {
                p.next = p1;
                p1 = p1.next;
            }
            p = p.next;
        }
        if (p1 != null) {
            p.next = p1;
        }

        if (p2 != null) {
            p.next = p2;
        }

        return dummy.next;

    }
}

class ListNode21 {
    public int val;
    public ListNode21 prev;
    public ListNode21 next;
}


